/**   
 * 文件名：TopicAction.java</br>
 * 描述： </br>
 * 开发人员：汪浩博 </br>
 * 创建时间： 2016-6-4
 */ 

package com.topic.action;

import java.util.List;

import net.sf.json.JSONObject;

import com.base.BaseAction.BaseAction;
import com.base.util.Page;
import com.opensymphony.xwork2.ModelDriven;
import com.post.bean.PostVO;
import com.post.service.PostService;
import com.topic.bean.Topic;
import com.topic.bean.TopicVO;
import com.topic.service.TopicService;
import com.user.bean.BBSUser;

/** 
 * 类名: TopicAction</br> 
 * 包名：com.topic.action </br> 
 * 描述: </br>
 * 发布版本号：</br>
 * 开发人员： 汪浩博</br>
 * 创建时间： 2016-6-4 
 */

public class TopicAction extends BaseAction implements ModelDriven<Topic>{

	private static final long serialVersionUID = 8689086434319701160L;
	
	private TopicService topicService;
	private PostService postService;
	private Topic topic = new Topic();
	private Integer pagenum = 1;
	
	/**
	 * 方法名: getPageResult</br>
	 * 详述: 后台查询分页主题</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-4</br>
	 * @return
	 */
	public String getPageResult(){
		Page querypage = topicService.Querypage(topic, pagenum, pagesize);
		//将结果集转换成VO
		TopicVO topicVO = new TopicVO();
		List queryResult = querypage.getQueryResult();
		List<TopicVO> list = topicVO.beanTransVo(queryResult,postService);
		//将原有的结果集替换成VO结果集
		querypage.setQueryResult(list);
		this.getRequest().setAttribute("page", querypage);
		return "backlist";
	}
	/**
	 * 方法名: getAllTopic</br>
	 * 详述: 查询所有主题</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-9</br>
	 * @return
	 */
	public String getAllTopic(){
		Page querypage = topicService.Querypage(topic, pagenum, 100);//偷个懒，将每页大小设为100，查询所有主题
		//将结果集转换成VO
		TopicVO topicVO = new TopicVO();
		List queryResult = querypage.getQueryResult();
		List<TopicVO> list = topicVO.beanTransVo(queryResult,postService);
		//将原有的结果集替换成VO结果集
		querypage.setQueryResult(list);
		this.getRequest().setAttribute("page", querypage);
		return "alltopic";
	}
	/**
	 * 方法名: addTopic</br>
	 * 详述: 新增主题</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-4</br>
	 * @return
	 */
	public String addTopic(){
		boolean addTopic = topicService.addTopic(topic);
		if(addTopic){
			this.getRequest().setAttribute("add_flag", addTopic);
		}
		return "addtopic";
	}
	/**
	 * 方法名: getSingleTopic</br>
	 * 详述: 通过id获取单个主题</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-5</br>
	 * @return
	 */
	public String getSingleTopic(){
		String id = this.getRequest().getParameter("id");
		Topic singleTopic = topicService.getSingleTopic(id);
		this.getRequest().setAttribute("topic", singleTopic);
		return "singletopic";
	}
	/**
	 * 方法名: getTopicById</br>
	 * 详述: 异步加载获取Topic,和版主信息</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-9</br>
	 * @return
	 */
	public String getTopicById(){
		String id = this.getRequest().getParameter("topicId");
		Topic singleTopic = topicService.getSingleTopic(id);
		Long postCountByTopic = postService.getCountByTopic(Long.parseLong(id));
		List<BBSUser> moderator = topicService.getModerator(id);
		JSONObject json = new JSONObject();
		json.put("singleTopic", singleTopic);
		json.put("postCountByTopic", postCountByTopic);
		json.put("moderator", moderator);
		this.writeJSON(json.toString());
		return null;
	}
	/**
	 * 方法名: updateTopic</br>
	 * 详述: 修改主题</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-5</br>
	 * @return
	 */
	public String updateTopic(){
		topicService.updateTopic(topic);
		return "updatetopic";
	}
	/**
	 * 方法名: deleteTopic</br>
	 * 详述: 通过id删除主题</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-5</br>
	 * @return
	 */
	public String deleteTopic(){
		String id = this.getRequest().getParameter("id");
		topicService.deleteTopic(id);
		return "deletetopic";
	}
	
	
	public Integer getPagenum() {
		return pagenum;
	}
	public void setPagenum(Integer pagenum) {
		this.pagenum = pagenum;
	}
	public PostService getPostService() {
		return postService;
	}
	public void setPostService(PostService postService) {
		this.postService = postService;
	}
	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	@Override
	public Topic getModel() {
		return this.topic;
	}

}
